package cn.asterly.cicada.web.controller;

import cn.asterly.cicada.common.model.*;
import cn.asterly.cicada.model.Essay;
import cn.asterly.cicada.service.base.IBaseService;
import cn.asterly.cicada.service.impl.ArticleServiceImpl;
import cn.asterly.cicada.web.servicebase.ServiceBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author  likun
 * @Date 21:45 2019-02-20
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/article")
@Api(value = "ArticleController",description = "文章操作类")
public class ArticleController extends ServiceBean {

    private static Logger logger=Logger.getRootLogger();

//    @Autowired
//    ArticleServiceImpl articleService;


    /**
     * @Description
     * @Date 21:45 2019-02-20
     * @Param [curentPage]
     * @return cn.asterly.cicada.common.model.DataTablesResult
     **/
    @ApiOperation(value = "分页查询文章",httpMethod="GET")
    @RequestMapping(value = "selaritcle/{curentPage}")
    public DataTablesResult selpaging(@PathVariable int curentPage){
        DataTablesResult atresult=new DataTablesResult();
        List<Essay> essays = articleService.selectByAll(curentPage, PagingSize.s_10);
        atresult.setSuccess(true);
        atresult.setData(essays);
        return atresult;
    }

    @ApiOperation(value = "根据id查询指定文章",httpMethod="GET")
    @RequestMapping(value = "selbyid/{id}")
    public ResponseEntity selById(@PathVariable String id){
        return new ResponseEntity(articleService.selectByAll(1,2), HttpStatus.OK);
    }

    /*
     * @Description
     * @Date 21:52 2019-02-20
     * @Param [essay]
     * @return cn.asterly.cicada.common.model.Result
     **/
    @ApiOperation(value = "添加文章",httpMethod = "POST")
    @RequestMapping(value = {"/addarticle"})
    public Result addArticle(Essay essay){

        if(essay.getContent()!=null){
            boolean b = articleService.insertSelective(essay);
            if(b){
                return new Result(true, ResultMessge.ADDSUCCESS,5200);
            }
        }

        return new Result(false, ResultMessge.ADDERROR,5404);

    }

    /*
     * @Description
     * @Date 21:52 2019-02-20
     * @Param [essay]
     * @return cn.asterly.cicada.common.model.Result
     **/
    @ApiOperation(value = "编辑/修改文章",httpMethod = "POST")
    @RequestMapping(value = {"/editarticle","/updarticle"},method = RequestMethod.POST)
    public Result editArticle(Essay essay){

        if(essay.getContent()!=null){
            boolean b = articleService.updateByPrimaryKey(essay);
            if(b){
                return new Result(true, ResultMessge.EDITSUCCESS,5200);
            }
        }

        return ResultStatus.editerror();

    }

    /*
     * @Description
     * @Date 21:52 2019-02-20
     * @Param [id]
     * @return cn.asterly.cicada.common.model.Result
     **/
    @ApiOperation(value = "根据id删除指定文章",httpMethod = "DELETE")
    @RequestMapping(value="/delbyid/{id}",method = RequestMethod.DELETE)
    public Result delById(@PathVariable String id){
        if(id!=null && !id.equals("")){
           boolean b=articleService.deleteByPrimaryKey(id);
           if(b){
            return new Result(true,ResultMessge.DELETESUCCESS,5200);
           }
        }else{
            logger.debug("id为空");
        }
        return new Result(false,ResultMessge.DELETEERROR,5404);

    }

    /**
     * @Description
     * @Date 21:52 2019-02-20
     * @Param [id]
     * @return cn.asterly.cicada.common.model.Result
     **/
    @RequestMapping(value="/delbyids")
    @ApiOperation(value = "根据id批量删除文章",httpMethod = "DELETE")
    public Result delByIds(@PathVariable String[] ids){

        if(ids!=null && !ids.equals("")){
            boolean b=articleService.deleteByPrimaryKeys(ids);
            if(b){
                return ResultStatus.delsuccess();
            }
        }else{
            logger.debug("id为空");
        }
        return ResultStatus.delerror();

    }

}
